package com.example.treehole;

import android.app.Application;

import com.baidu.mapapi.SDKInitializer;
import com.example.treehole.bean.User;
import com.example.treehole.database.database.Database;
import com.example.treehole.util.PreferenceUtil;
import com.qweather.sdk.JWTGenerator;
import com.qweather.sdk.QWeather;


public class App extends Application {
    static App context;
    public static User user;


    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        boolean first = PreferenceUtil.getInstance().get("first", true);
        if (first) {
            PreferenceUtil.getInstance().save("first", false);
        }
        SDKInitializer.setAgreePrivacy(this,true);
        SDKInitializer.initialize( this);
        user = PreferenceUtil.getInstance().get("logger", User.class);
        Database.init(this);
        QWeather instance = QWeather.getInstance(this, "n43qqkk3w4.re.qweatherapi.com")
                .setLogEnable(true);
        // 通过SDK提供的JWTGenerator设置令牌生成器，其实现自TokenGenerator接口
        JWTGenerator jwt = new JWTGenerator(
                "-----BEGIN PRIVATE KEY-----\n" +
                        "MC4CAQAwBQYDK2VwBCIEIJIJurFYcqPJIy6j3R4f9k3EQq9MJo+URWY2mfNr1LiM\n" +
                        "-----END PRIVATE KEY-----\n" , // 私钥
                "4NKPCF84RT", // 项目ID
                "KKPM9BXNVB"); // 凭据ID
        instance.setTokenGenerator(jwt);
    }

    public static App getContext() {
        return context;
    }


    public static boolean isLogin() {
        return user != null;
    }


    public static void login(User user_) {
        user = user_;
        PreferenceUtil.getInstance().save("logger", user_);
    }

    public static void logout() {
        user = null;
        PreferenceUtil.getInstance().remove("logger");
    }
}
